package com.example.sys.controller;


import cn.hutool.core.bean.BeanUtil;
import com.example.config.ResponseBean;
import com.example.config.UnicomResponseEnums;
import com.example.sys.entity.Notice;
import com.example.sys.service.NoticeService;
import com.example.sys.vo.NoticeVo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 通知公告表 前端控制器
 * </p>
 *
 * @author anan
 * @since 2019-06-20
 */
@RestController
@RequestMapping("/sys/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/getNoticeList")
    public ResponseBean<UnicomResponseEnums> getNoticeList(NoticeVo noticeVo){
        System.out.println(noticeVo);
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(noticeVo, false, true);
        if (stringObjectMap.isEmpty()){
            List<Notice> configs = noticeService.list();
            return new ResponseBean(true, configs, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(true, noticeService.selectPage(noticeVo), UnicomResponseEnums.SUCCESS_OPTION);
    }

    @DeleteMapping("/deleteByNoticeId")
    public ResponseBean<UnicomResponseEnums> deleteByNoticeId(@RequestBody NoticeVo noticeVo){
        if (noticeVo.getNoticeId()!=null && noticeService.removeById(noticeVo.getNoticeId())){
            return new ResponseBean(true,UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(true, UnicomResponseEnums.OPERATION_FAILED);
    }

    @PostMapping("/updateNoticeById")
    public ResponseBean<UnicomResponseEnums> updateNoticeById(@RequestBody Notice notice){
        if (notice.getNoticeId()!=null && noticeService.updateById(notice)){
            return new ResponseBean(true,UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(true, UnicomResponseEnums.OPERATION_FAILED);
    }

    @PostMapping("/addNotice")
    public ResponseBean<UnicomResponseEnums> addNotice(@RequestBody @Valid Notice notice){
        if (!notice.toString().isEmpty()) {
            noticeService.save(notice);
            return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(false, UnicomResponseEnums.OPERATION_FAILED);
    }

}
